<template>
  <div>
    <el-dialog :visible="true" title="添加" :before-close="callOf" center>
      <el-form ref="form" :model="sizeForms" :rules="formrules" label-width="80px" size="mini">
        <el-form-item label="任务名称" prop="task_name">
          <el-input ref="task_name" v-model="sizeForms.task_name" placeholder="请输入任务名称" />
        </el-form-item>
        <el-form-item label="工作名称" prop="job_name">
          <el-input v-model="sizeForms.job_name" placeholder="请输入工作名称" />
        </el-form-item>
        <el-form-item label="token" prop="ding_token">
          <el-input v-model="sizeForms.ding_token" type="textarea" :rows="2" placeholder="请输入token值" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="dialogFormAdd('form')">新增</el-button>
          <el-button type="primary" @click="callOf()">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
import { add_task } from '@/api/ci'
export default {
  name: 'Task',
  data() {
    return {
      sizeForms: {
        task_name: '',
        job_name: '',
        ding_token: ''
      },
      formrules: {
        task_name: [{ required: true, message: '任务名称不能为空', trigger: 'blur' }],
        job_name: [{ required: true, message: '工作名称不能为空', trigger: 'blur' }],
        ding_token: [{ required: true, message: 'token不能为空', trigger: 'blur' }]
      }
    }
  },
  methods: {
    callOf() {
      this.$emit('closePop')
    },
    dialogFormAdd(form) {
      this.$refs[form].validate((valid) => {
        if (valid) {
          var data = new URLSearchParams()
          var task_name = this.sizeForms.task_name
          var job_name = this.sizeForms.job_name
          var ding_token = this.sizeForms.ding_token
          data.append('task_name', task_name)
          data.append('job_name', job_name)
          data.append('ding_token', ding_token)
          add_task(data)
            .then(res => {
              this.$message({
                type: 'success',
                message: '添加成功'
              })
              this.sizeForms = { task_name: '', job_name: '', ding_token: '' }
              this.$emit('closePop', 'success')
            })
          this.sizeForms = {}
        } else {
          this.$message({
            type: 'error',
            message: '添加失败'
          })
        }
      })
    }
  }
}

</script>
